package org.lzy.shop.order

import android.view.View
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textview.MaterialTextView
import com.kongzue.dialogx.dialogs.BottomDialog
import com.kongzue.dialogx.interfaces.OnBindView
import org.lzy.shop.Constant
import org.lzy.shop.R
import org.lzy.shop.adapter.OrderRefundReasonAdapter
import org.lzy.shop.adapter.OrderSpecAdapter
import org.lzy.shop.base.BaseMvvmActivity
import org.lzy.shop.databinding.ActivityOrderrefundBinding
import org.lzy.shop.viewmodel.OrderModel

/**
 * 退款申请
 */
class OrderRefundActivity: BaseMvvmActivity<ActivityOrderrefundBinding, OrderModel>({ ActivityOrderrefundBinding.inflate(it) })  {
    var orderID = 0

    override fun initView() {
        setTitleText(getResString(R.string.order_refund))
        if (intent.extras != null) {
            val bundle = intent.extras
            orderID = bundle!!.getInt(Constant.ORDERID)

        }

    }

    override fun requestData() {
        mViewModel.getOrderInfo(orderID)

    }

    override fun initData() {
        mViewModel.orderInfoData.observeWithLifecycle(this){orderInfoResponse ->
            orderInfoResponse?.let {
                val orderSpecAdapter = OrderSpecAdapter()
                binding.shopTv.apply {
                    layoutManager= LinearLayoutManager(this@OrderRefundActivity, LinearLayoutManager.VERTICAL, false)
                    adapter=orderSpecAdapter
                }
                orderSpecAdapter.submitList(orderInfoResponse.goodsList)

                binding.refundMoney.text=orderInfoResponse.price.toString()
            }

        }

        var reasonShow: String?=null
        //退款原因回调
        mViewModel.getDataReasonAddData.observeWithLifecycle(this){orderRefundReasons ->
            orderRefundReasons?.let {
                BottomDialog.show(object : OnBindView<BottomDialog?>(R.layout.dialog_orderrefund) {
                    override fun onBind(dialog: BottomDialog?, view: View) {
                        val couponRv = view.findViewById<RecyclerView>(R.id.recyclerview)
                        val sureReason = view.findViewById<MaterialTextView>(R.id.refund_submit)
                        val orderRefundReasonAdapter  = OrderRefundReasonAdapter()
                        couponRv.apply {
                            layoutManager=LinearLayoutManager(this@OrderRefundActivity, LinearLayoutManager.VERTICAL, false)
                            adapter=orderRefundReasonAdapter
                        }
                        orderRefundReasonAdapter.submitList(orderRefundReasons)
                        orderRefundReasonAdapter.setOnItemClickListener { adapter, view, position ->
                            val item = adapter.getItem(position)
                            item?.let {
                                orderRefundReasonAdapter.setSelectedPosition(position)
                                orderRefundReasonAdapter.notifyDataSetChanged()
                                reasonShow=item.name

                            }

                        }
                        sureReason.setOnClickListener {
                            dialog?.dismiss()
                            binding.showRefundReason.text=reasonShow
                        }
                    }
                }).setMaskColor("#4D000000".toColorInt())

            }
        }

        mViewModel.orderRefundData.observe(this){
            dismissLoading()
            showSuccessToast(getResString(R.string.refund_success))
            finish()
        }

    }

    override fun allClick() {
        binding.chooseRefundReason.setOnClickListener {
            mViewModel.getReasonData()
        }
        //退货申请
        binding.refundSubmit.setOnClickListener {
            if (binding.showRefundReason.text.toString() == "请选择"||binding.showRefundReason.text.toString() == "Please Choose") {
               showToast(getResString(R.string.refund_reason))
            }else{
                showLoading()
                mViewModel.refundOrder(orderID,binding.showRefundReason.text.toString())
            }

        }

    }

}